package mapreduceFromToDb;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.mapreduce.lib.db.DBWritable;

public class PersonRecorder implements Writable, DBWritable {
	public int id;
	public String name;
	public int age;

	@Override
	public void write(PreparedStatement stmt) throws SQLException {
		stmt.setInt(1, id);
		stmt.setString(2, name);
		stmt.setInt(3, age);

	}

	@Override
	public void readFields(ResultSet result) throws SQLException {
		this.id = result.getInt(1);
		this.name = result.getString(2);
		this.age = result.getInt(3);

	}

	@Override
	public void write(DataOutput out) throws IOException {
		out.write(id);
		Text.writeString(out, this.name);
		out.writeInt(this.age);

	}

	@Override
	public void readFields(DataInput in) throws IOException {
		this.id = in.readInt();
		this.name = Text.readString(in);
		this.age = in.readInt();

	}

	@Override
	public String toString() {
		return "id:" + this.id + "age:" + this.age + "name:" + name;
	}

}
